//
//  SimRequest.h
//  TKnowBox
//
//  Created by LiuXubin on 16/5/7.
//  Copyright © 2016年 knowin. All rights reserved.
//

#import <AFNetworking/AFNetworking.h>
#import "SimDefine.h"

typedef NS_ENUM(NSInteger, RequestLoadType){
    RLT_None,
    RLT_EmptyLoad,
    RLT_OverlayLoad,
};

typedef NS_ENUM(NSInteger, CacheDataReturnType){
    CDRT_FailureReturn,
    CDRT_ImmediateReturn, //是否立即返回缓存数据，注意：如果请求成功，请求数据后后续再返回
};

NS_ASSUME_NONNULL_BEGIN

extern NSString * const kLoadingType;
extern NSString * const kLoadingView;
extern NSString * const kReturnType;

SimDefineValue(kSimRequestBaseInfoDict, NSDictionary *);
SimDefineValue(kSimRequestCacheDir, NSString *);
SimDefineValue(kSimRequestTimeOut, int);


@interface SimRequest : AFHTTPSessionManager

+ (void)setServerAddress:(NSString *)address;
+ (NSString *)serverAddress;
+ (void)setWebServerAddress:(NSString *)address;
+ (NSString *)webServerAddress;

//数据请求
+ (NSURLSessionDataTask *)requestPath:(NSString *)path
                           loadingDic:(NSDictionary *)loadingDic
                            postParam:(NSDictionary * _Nullable)postParam
                             getParam:(NSDictionary * _Nullable)getParam
                                cache:(BOOL)cache
                              success:(void (^)(SimRequest *request,  id jsonData))success
                              failure:(void (^)(SimRequest *request, NSError *error))failure;

+ (NSURLSessionDataTask *)requestPath:(NSString *)path
                           loadingDic:(NSDictionary * _Nullable)loadingDic
                           queryParam:(NSDictionary * _Nullable)param
                              success:(void (^)(SimRequest *request,  id jsonData))success
                              failure:(void (^)(SimRequest *request, NSError *error))failure;

+ (NSURLSessionDataTask *)requestPath:(NSString *)path
                           loadingDic:(NSDictionary * _Nullable)loadingDic
                            postParam:(NSDictionary * _Nullable)param
                              success:(void (^)(SimRequest *request,  id jsonData))success
                              failure:(void (^)(SimRequest *request, NSError *error))failure;

+ (NSURLSessionDataTask *)requestPath:(NSString *)path
                           loadingDic:(NSDictionary * _Nullable)loadingDic
                           queryParam:(NSDictionary * _Nullable)param
                                cache:(BOOL)cache
                              success:(void (^)(SimRequest *request,  id jsonData))success
                              failure:(void (^)(SimRequest *request, NSError *error))failure;

//下载
+ (NSURLSessionDownloadTask *)download:(NSString *)urlStr toPath:(NSString *)filePath progressBlock:(void (^)(NSProgress * _Nonnull downloadProgress))grogressBlock complete:(CompleteBlock)complete;


/*
 *post和query参数对应的key值返回，加密逻辑在这块重写
 */
- (NSString *)queryStrAddSaltForQueryParam:(NSMutableDictionary *)queryParams;
- (NSString *)postStrAddSaltForPostParam:(NSMutableDictionary *)postParams;

/*
 *请求缓存存储逻辑
 */
+ (NSString *)dirForCacheReqeust;
+ (void)clearCache;

/*
 *请求loading处理
 */
+ (void)showLoadingForType:(RequestLoadType)type inView:(nullable UIView *)view;
+ (void)dismissLoadingInView:(nullable UIView *)view;

/*
 *json返回处理
 */
+ (BOOL)isValidateJsonForJsonDict:(nullable id)jsonDict;
+ (BOOL)isSuccessJsonForJsonDict:(nullable id)jsonDict;
+ (nullable id)handleSuccessJson:(nullable id)json;
+ (nullable NSError *)errorForValidateJson:(nullable id)json;
+ (BOOL)dealWithErrorCodeJson:(nullable id)json;
+ (nullable NSDictionary *)baseInfoDict:(nullable NSDictionary *)dict;

+ (nonnull NSString *)keyForURLString:(nonnull NSString *)urlString;
+ (nullable NSMutableString *)queryStrWithParam:(nullable NSDictionary *)queryParam;

NS_ASSUME_NONNULL_END

@end

